<!DOCTYPE html>
<html xmlns:layout="http://www.ultraq.net.nz/web/thymeleaf/layout" layout:decorate="~{admin/layout}">

<head>
	<title>Review Manage</title>
	<style>

	</style>
</head>

<body layout:fragment="content">
	<div class="card shadow">
		<div class="card-header border-bottom d-flex justify-content-between align-items-center">
			<h5 class="card-title mb-0">Review List Of [[${sport.sportTitle}]]
				<a class="btn btn-sm btn-dark" th:href="@{/admin/sport}">Back</a>
			</h5>
			<div class="d-grid gap-2 d-md-block">
				<!--<button class="btn btn-sm btn-primary" type="button" onclick="resetReviewModal('')">Reset</button>-->
			</div>
		</div>
		<div class="card-body">
			<table id="reviewTable"></table>
		</div>
		<!--<div class="card-footer pt-0">
			Footer
		</div>-->
	</div>
	<!---->
	<div class="modal fade" id="reviewModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
		aria-hidden="true">
		<div class="modal-dialog modal-lg">
			<div class="modal-content">
				<div class="modal-header">
					<h4 class="modal-title" id="myModalLabel">Review Reply</h4>
				</div>
				<div class="modal-body">
					<div class="row">
						<div class="col-md-12">
							<form id="reviewForm">
								<input type="hidden" class="form-control" value="" id="reviewId" name="reviewId">
								<div class="mb-3">
									<label class="form-label">Reply-Text</label>
									<input type="text" class="form-control" value="" id="reviewReply" name="reviewReply">
								</div>
								<!-- Save button -->
								<div class="d-flex justify-content-end mt-4">
									<button type="button" class="btn btn-default"
										onclick="closeReviewModal()">Close</button>
									<button type="submit" class="btn btn-primary">Submit Reply</button>
								</div>
							</form>
						</div>
					</div>
				</div>
			</div><!-- /.modal-content -->
		</div><!-- /.modal -->
	</div>
	<script layout:fragment="script">
		var reviewFormValidate = null, sportId = '[[${sportId}]]'
		$(document).ready(function () {
			reviewFormValidate = 
			$("#reviewForm").validate({
				rules: {
					reviewId: {
						required: true
					},
					reviewReply: {
						required: true
					}
				},
				submitHandler: function (form) {
					var formData = $(form).serialize();
					saveReview(formData);
					return false
				}
			})
			loadReviewTable()
		});
		function loadReviewTable() {
			$('#reviewTable').bootstrapTable('destroy');
			$('#reviewTable').bootstrapTable({
				url: "[[@{'/admin/reviews'}]]",
				uniqueId: 'reviewId',
				dataType: "json",
				pagination: true,
				pageSize: 10,
				queryParams: function (params) {
					var temp = {
						size: params.limit,
						page: (params.offset / params.limit),
						sort: params.sort,
						sortOrder: params.order,
						sportId: sportId
					};
					return temp;
				},
				sidePagination: "server",
				pageList: [30, 50],
				columns: [{
					title: 'Sort',
					formatter: function (value, row, index) {
						var pageSize = $('#reviewTable').bootstrapTable('getOptions').pageSize;
						var pageNumber = $('#reviewTable').bootstrapTable('getOptions').pageNumber;
						return pageSize * (pageNumber - 1) + index + 1;
					}
				}, {
					field: 'reviewUserId',
					title: 'Review-User',
					formatter: function (value, row, index) {
						return row.user.userName
					}
				}, {
					field: 'reviewText',
					title: 'review-Text',
					formatter: function (value, row, index) {
						return value + '</br>' + row.reviewTime
					}
				}, {
					field: 'reviewReply',
					title: 'Reply',
					formatter: function (value, row, index) {
						if(value){
							return value + '</br>' + row.reviewReplyTime
						}
						return ''
					}
				}, {
					title: 'Operate',
					field: 'reviewId',
					class: 'text-center',
					width: '20%',
					formatter: function (value, row, index) {
						let res = '<button class="btn btn-sm btn-warning" onclick="modifyReviewModal(\'' + value + '\')">Reply</button>&nbsp;';
						res += '<button class="btn btn-sm btn-danger " onclick="removeReview(\'' + value + '\')">Remove</button>';
						return res;
					}
				},],
				onPostBody: function (value) {
					$(".fixed-table-container").css('padding-bottom', '37px');
				}
			});
		}
		function closeReviewModal() {
			reviewFormValidate.resetForm()
			$('#reviewModal').modal('hide')
		}
		function modifyReviewModal(reviewId) {
			var data = $("#reviewTable").bootstrapTable('getRowByUniqueId', reviewId);
			$('#reviewId').val(data.reviewId)
			$('#reviewReply').val(data.reviewReply)
			$('#reviewModal').modal('show')
		}
		function saveReview(data) {
			$.post(ctx + "/admin/saveReview", data, function (resp) {
				if (resp.status == 0) {
					loadReviewTable();
					$('#reviewModal').modal('hide');
				}
				layer.msg(resp.msg);
			})
		}
		function removeReview(reviewId) {
			layer.msg("Do you want to delete it", {
				title: 'tip', icon: 3, btn: ["Confirm", "Cancel"],
				yes: function () {
					$.ajax({
						type: "get",
						url: ctx + "/admin/removeReview",
						data: {
							"reviewId": reviewId,
						},
						success: function (resp) {
							if (resp.status == 0) {
								loadReviewTable()
							}
							layer.msg(resp.msg);
						}
					});
				},
				btn2: function () {
					// layer.msg("Cancel");
				}
			});
		}
	</script>
</body>

</html>